﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Collections;

namespace Notifying
{
    public static class EmailBuilder
    {

        /*TODO: Make 2 methods. One for building query results 
        public static string BuildResultHtml_SingleQueryInDocument(User User, string Query, List<Document> Results, HebrewStemmer hs)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(String.Format("<cfmail type=\"HTML\"> Dear {0} , Relevant links regarding query : {1} <br><br>", 
                User.FirstName, Query));

            foreach (Document doc in Results)
                sb.Append(String.Format("{2} <br> {0} <br> {1} <br> {3} <br> <hr> <br>", 
                    doc.Fields[(int)Common.Document.Section.Title].Text, 
                    doc.Url.ToString(), doc.DateTime.ToString(), ArrangeExcerpts(doc.GetHTMLExcerpts(Query, Document.Section.Text, 1, 5, 5, hs))));

            sb.Append(String.Format("Update Issued At : {0} </cfmail>", DateTime.Now.ToString()));
            return sb.ToString();
        }
        */
        public static string BuildResultHTML_AllQueriesInDocument(User User, Hashtable Results, HebrewStemmer hs)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(String.Format("<cfmail type=\"HTML\"> Dear {0} , Relevant links :  <br><br>",
                User.FirstName));

            foreach (DictionaryEntry docQueryList in Results)
            {
                Document doc = docQueryList.Key as Document;
                string[] QueryList = (docQueryList.Value as List<string>).ToArray();
                sb.Append(String.Format("{2} <br> {0} <br> {1} <br> {3} <br> <hr> <br>",
                    doc.Fields[(int)Common.Document.Section.Title].Text,
                    doc.Url.ToString(), doc.DateTime.ToString(), ArrangeExcerpts(doc.GetHTMLExcerpts(QueryList, Document.Section.Text, 1, 5, 5, hs))));

            }
            sb.Append(String.Format("Update Issued At : {0} </cfmail>", DateTime.Now.ToString()));
            return sb.ToString();

        }
        
        private static string ArrangeExcerpts(string[] Excerpts)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in Excerpts)
            {
                sb.Append(String.Format("{0}<br>", s));
            }
            return sb.ToString();
        }
         
    }
}
